package com.hisense.hiioc.hismartplate.server;

import com.alibaba.fastjson.JSONObject;

/**
 * @Author: almo
 * @Date: 2018/12/6 上午11:18
 * @Version 1.0
 */
public class ProtoBuilder {

    public static final int PING_PROTO = 1 << 8 | 220; //ping消息
    public static final int PONG_PROTO = 2 << 8 | 220; //pong消息
    public static final int SYST_PROTO = 3 << 8 | 220; //系统消息
    public static final int EROR_PROTO = 4 << 8 | 220; //错误消息
    public static final int AUTH_PROTO = 5 << 8 | 220; //认证消息
    public static final int MESS_PROTO = 6 << 8 | 220; //普通消息

    public static String buildPingProto() {
        return buildProto(PING_PROTO, null);
    }

    public static String buildPongProto() {
        return buildProto(PONG_PROTO, null);
    }

    public static String buildSystProto(String command) {
        return buildProto(SYST_PROTO, command);
    }

    public static String buildAuthProto(boolean isSuccess) {
        Proto<Boolean> proto = new Proto<>(AUTH_PROTO, null);
        proto.setExtension(isSuccess);
        return JSONObject.toJSONString(proto);
    }

    public static String buildErorProto(int code, String msg) {
        return buildProto(EROR_PROTO, msg);
    }

    public static <T> String buildMessProto(String cmd, T extension) {
        Proto<T> proto = new Proto<>(MESS_PROTO, cmd);
        proto.setExtension(extension);
        return JSONObject.toJSONString(proto);
    }

    private static String buildProto(int head, String body) {
        Proto proto = new Proto(head, body);
        return JSONObject.toJSONString(proto);
    }

}
